package com.gis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.entity.FloatAnimalData;
import com.gis.result.ResponseResult;
import com.gis.service.FloatAnimalDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequiredArgsConstructor
@RequestMapping("/float_animal_data")
@Tag(name = "浮游动物", description = "浮游动物数据接口")
@CrossOrigin(origins = "*")
public class FloatAnimalDataController {
    @Autowired
    private FloatAnimalDataService floatAnimalDataService;

    @PostMapping("/addBatch")
    @Operation(summary = "批量添加浮游动物数据")
    @Parameters({
        @Parameter(name = "floatAnimalDataList", description = "浮游动物数据列表", required = true)
    })
    public ResponseResult<String> AddFloatAnimalDataServeice(@RequestBody List<FloatAnimalData> floatAnimalDataList) {
        floatAnimalDataService.saveBatch(floatAnimalDataList);
        // floatAnimalDataService.save(floatAnimalData);
        return ResponseResult.success("insert successfully");
    }

    @GetMapping("/all")
    public ResponseResult<List<FloatAnimalData>> GetAllFloatAnimalData() {
        List<FloatAnimalData> floatAnimalDatas = floatAnimalDataService.list();
        return ResponseResult.success(floatAnimalDatas);
    }

    @GetMapping("/getByTime")
    @Operation(summary = "根据年份和季度获取浮游动物数据")
    @Parameters({
        @Parameter(name = "year", description = "年份", required = true),
        @Parameter(name = "season", description = "季度", required = true)
    })
    public ResponseResult<List<FloatAnimalData>> GetByTime(@RequestParam Integer year, @RequestParam String season) {
        QueryWrapper<FloatAnimalData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year).eq("season", season);
        List<FloatAnimalData> roeDatas = floatAnimalDataService.list(queryWrapper);
        return ResponseResult.success(roeDatas);
    }

    @Operation(summary = "更新浮游动物数据")
    @Parameters({
        @Parameter(name = "floatAnimalData", description = "浮游动物数据", required = true)
    })
    @PostMapping("/update")
    public ResponseResult<FloatAnimalData> UpdateFloatAnimalData(@RequestBody FloatAnimalData floatAnimalData) {
        floatAnimalDataService.saveOrUpdate(floatAnimalData);
        return ResponseResult.success(floatAnimalData);
    }

    @Operation(summary = "根据ID删除浮游动物数据")
    @Parameters({
        @Parameter(name = "id", description = "浮游动物数据ID", required = true)
    })
    @PostMapping("/deleteById")
    public ResponseResult<String> DeleteBenthosDataById(@RequestBody Object json) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(json));
        Integer id = jsonObject.getInteger("id");
        floatAnimalDataService.removeById(id);
        return ResponseResult.success("delete successfully");
    }

    @Operation(summary = "分页获取浮游动物数据")
    @Parameters({
        @Parameter(name = "pageNo", description = "页码", required = true),
        @Parameter(name = "pagesize", description = "每页数量", required = true)
    })
    @GetMapping("/listPage")
    public ResponseResult<IPage<FloatAnimalData>> ListPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        IPage<FloatAnimalData> page = new Page<>(pageNo, pageSize, true);
        return ResponseResult.success(floatAnimalDataService.page(page));
    }
}